<template>
    <a-modal
        :title="(!currRow ? '添加' : '编辑') + '行政区划'"
        :visible="isShowModal"
        width="40%"
        :footer="null"
        dialogClass="custom-modal"
        @cancel="handleCancel"
        :maskClosable="false"
    >
        <a-form-model
            :model="form"
            ref="form"
            :labelCol="{ span: 5 }"
            :wrapperCol="{ span: 19 }"
            :rules="rules"
            :colon="false"
        >
            <a-row :gutter="10">
                <a-col :span="24" class="mb16">
                    <a-form-model-item label="名称" prop="name">
                        <a-input placeholder="请输入名称" v-model.trim="form.name" allowClear />
                    </a-form-model-item>
                </a-col>

                <a-col :span="24" class="mb16">
                    <a-form-model-item label="别名" prop="abbreviation">
                        <a-input placeholder="请输入别名" v-model.trim="form.abbreviation" allowClear />
                    </a-form-model-item>
                </a-col>

                <a-col :span="24" class="mb16">
                    <a-form-model-item label="行政编码" prop="code">
                        <a-input-number style="width: 100px" placeholder="请输入行政编码" v-model="form.code" />
                        <!--                        <a-input-number-->
                        <!--                            style="width: 100px"-->
                        <!--                            placeholder="请输入行政编码"-->
                        <!--                            v-model="form.code"-->
                        <!--                            :min="510000"-->
                        <!--                            :max="513499"-->
                        <!--                        />-->
                    </a-form-model-item>
                </a-col>

                <a-col :span="24" class="mb16">
                    <a-form-model-item label="经度" prop="longitude">
                        <a-input-number
                            style="width: 200px"
                            placeholder="请输入经度"
                            v-model="form.longitude"
                            :min="97"
                            :max="109"
                        />
                    </a-form-model-item>
                </a-col>

                <a-col :span="24" class="mb16">
                    <a-form-model-item label="纬度" prop="latitude">
                        <a-input-number
                            style="width: 200px"
                            placeholder="请输入纬度"
                            v-model="form.latitude"
                            :min="26"
                            :max="35"
                        />
                    </a-form-model-item>
                </a-col>

                <a-col :span="24">
                    <a-button type="primary" class="fr" @click="handleOk" :loading="confirmLoading">确定</a-button>
                </a-col>
            </a-row>
        </a-form-model>
    </a-modal>
</template>
<script>
import { areaRestApi } from '../../../../common/api/setting'

export default {
    data() {
        return {
            confirmLoading: false,
            level: 1,
            form: {
                abbreviation: '',
                code: '',
                latitude: 0,
                longitude: 0,
                name: '',
                parentId: null,
            },
            rules: {
                name: [{ required: true, message: '请输入评价内容', trigger: 'blur' }],
                operationGuide: [{ required: true, message: '请输入评价操作指南', trigger: 'blur' }],
                score: [{ required: true, message: '请输入分值', trigger: 'blur' }],
                sortNum: [{ required: true, message: '请输入排序', trigger: 'blur' }],
            },
        }
    },
    components: {},
    props: {
        isShowModal: {
            required: true,
            type: Boolean,
        },
        currRow: {
            required: false,
            type: [Object, undefined],
        },
        parentRow: {
            required: false,
            type: [Object, undefined],
        },
    },
    watch: {
        isShowModal: {
            immediate: true,
            handler: function (isShowModal) {
                if (isShowModal) {
                    this.beforeShowModal()
                } else {
                    this.$refs.form && this.$refs.form.resetFields()
                }
            },
        },
    },
    beforeDestroy() {
        this.$refs.form && this.$refs.form.resetFields()
    },
    methods: {
        beforeShowModal() {
            if (this.currRow) {
                let { abbreviation, code, latitude, longitude, name, parentId } = this.currRow

                Object.assign(this.form, {
                    abbreviation,
                    code,
                    latitude,
                    longitude,
                    name,
                    parentId,
                })
            } else if (this.parentRow) {
                let { id } = this.parentRow
                console.log(level)
                Object.assign(this.form, {
                    parentId: id,
                })
            }
        },
        async save() {
            const apiType = this.currRow ? 'edit' : 'add'
            const { abbreviation, code, id, latitude, longitude, name, parentId } = this.form
            let ps = {
                abbreviation,
                code,
                id,
                latitude,
                longitude,
                name,
                parentId: parentId || this.parentRow?.id,
            }

            if (this.currRow) {
                ps['id'] = this.currRow.id
            }

            await areaRestApi[apiType](ps)
        },
        async handleOk() {
            this.confirmLoading = true
            this.$refs.form.validate(async valid => {
                console.log(valid, 'valid')
                if (valid) {
                    this.save()
                        .then(() => {
                            this.$emit('update:isShowModal', false)
                            this.$message.success('操作成功')
                            this.$emit('updateData')
                        })
                        .catch(e => {
                            console.log(e)
                            this.$message.error(e.message)
                        })
                        .finally(() => {
                            this.confirmLoading = false
                        })
                } else {
                    this.confirmLoading = false
                    return false
                }
            })
        },
        handleCancel() {
            this.$emit('update:isShowModal', false)
        },
    },
}
</script>
<style scoped></style>
